package main

/*
最小堆的实现
*/

type minHeap []int

func (h minHeap) Len() int {
	return len(h)
}

func (h minHeap) Less(i, j int) bool {
	//最小堆
	return h[i] < h[j]
	//最大堆
	//return h[i] < h[j]
}

/*
实现Swap 交换index i和index j的元素
*/
func (h *minHeap) Swap(i, j int) {
	(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

/*
实现push 结构将元素push到堆中
*/
func (h *minHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

/*
实现pop 结构将元素push到堆中
*/
func (h *minHeap) Pop() interface{} {
	res := (*h)[len(*h)-1]
	*h = (*h)[:len(*h)-1]
	return res
}

/*func main() {
	h := make(minHeap, 0)
	heap.Init(&h)

	heap.Push(&h, 2)
	heap.Push(&h, 7)
	heap.Push(&h, 5)
	heap.Push(&h, 5)
	heap.Push(&h, 4)
	heap.Push(&h, 6)

	for len(h) != 0 {
		fmt.Println(heap.Pop(&h))
	}
}
*/
